package com.example.ljw.paperlive.ui.main;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ljw.paperlive.R;
import com.example.ljw.paperlive.adapter.MyMainViewPagerAdapter;
import com.example.ljw.paperlive.ui.first.FirstFragment;
import com.example.ljw.paperlive.ui.famliy.FamliyFragment;
import com.example.ljw.paperlive.ui.friend.FriendFragment;
import com.example.ljw.paperlive.ui.live.LiveFragment;
import com.example.ljw.paperlive.ui.my.MyFragment;
import com.example.ljw.paperlive.utils.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //tv
    private TextView tv_title;
    //toolbar
    private Toolbar toolbar_main;
    //初始化导航的5个radiobtn
    private RadioButton radio_live_main;
    private RadioButton radio_friend_main;
    private RadioButton radio_begin_main;
    private RadioButton radio_famliy_main;
    private RadioButton radio_my_main;
    //fragment的集合
    private List<Fragment> mListFragment=new ArrayList<>();
    //noScrollViewPager 的适配器
    private MyMainViewPagerAdapter mAdapter;
    //noScrollViewPager
    private NoScrollViewPager noScrollViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initViewpager();
        initView();

    }

    private void initData() {
//        // 隐藏状态栏，全屏显示
//        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
//        getWindow().setFlags(flag, flag);
        FirstFragment firstFragment=new FirstFragment();
        mListFragment.add(firstFragment);
        FriendFragment friendFragment=new FriendFragment();
        mListFragment.add(friendFragment);
        LiveFragment liveFragment=new LiveFragment();
        mListFragment.add(liveFragment);
        FamliyFragment famliyFragment=new FamliyFragment();
        mListFragment.add(famliyFragment);
        MyFragment  myFragment=new MyFragment();
        mListFragment.add(myFragment);


    }

    private void initViewpager() {
        noScrollViewPager= (NoScrollViewPager) findViewById(R.id.noviewpager_main);
        mAdapter=new MyMainViewPagerAdapter(getSupportFragmentManager(),mListFragment);
        noScrollViewPager.setAdapter(mAdapter);
        noScrollViewPager.setOffscreenPageLimit(mListFragment.size()-1);
    }

    private void initView() {
        toolbar_main= (Toolbar) findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar_main);
        tv_title= (TextView) findViewById(R.id.tv_title);
        radio_live_main= (RadioButton) findViewById(R.id.radio_live_main);
        radio_friend_main= (RadioButton) findViewById(R.id.radio_friend_main);
        radio_begin_main= (RadioButton) findViewById(R.id.radio_begin_main);
        radio_famliy_main= (RadioButton) findViewById(R.id.radio_famliy_main);
        radio_my_main= (RadioButton) findViewById(R.id.radio_my_main);
        radio_live_main.setOnClickListener(this);
        radio_friend_main.setOnClickListener(this);
        radio_begin_main.setOnClickListener(this);
        radio_famliy_main.setOnClickListener(this);
        radio_my_main.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.radio_live_main:
                noScrollViewPager.setCurrentItem(0);
                break;
            case R.id.radio_friend_main:
                noScrollViewPager.setCurrentItem(1);
                break;
            case R.id.radio_begin_main:
                noScrollViewPager.setCurrentItem(2);
                break;
            case R.id.radio_famliy_main:
                toolbar_main.setNavigationIcon(R.mipmap.search);
                tv_title.setText("家族");
                noScrollViewPager.setCurrentItem(3);
                break;
            case R.id.radio_my_main:
                noScrollViewPager.setCurrentItem(4);
                break;
        }
    }
}
